package com.shijun.im.activity.presenter.impl;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.shijun.im.R;
import com.shijun.im.activity.RegistActivity;
import com.shijun.im.activity.activity.RegistView;
import com.shijun.im.activity.interactor.RegistInteractor;
import com.shijun.im.activity.interactor.impl.RegistInteractorImpl;
import com.shijun.im.activity.presenter.RegistPresenter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhushijun on 2017/10/19 0019.
 */

public class RegistPresenterImpl implements RegistPresenter, RegistInteractor.OnRegistFinishedListener, RegistInteractor.OnRegistInputListener, RegistInteractor.OnAvatarListener {

    private RegistView registView;
    private RegistInteractor registInteractor;
    private Context context;
    private boolean isCanRegist = false;
    private Map<String, String> userInfo = new HashMap<>();
    private Uri tempUri = null;

    public RegistPresenterImpl() {
    }

    public RegistPresenterImpl(Context context, RegistView registView) {
        this.registView = registView;
        this.context = context;
        this.registInteractor = new RegistInteractorImpl(context);
        tempUri = this.registInteractor.getTempUri();
        Log.i("TAG","tempUri是否为空："+tempUri);
    }

    @Override
    public void validateCredentials(Map<String, String> userInfo) {
        registInteractor.registInput(userInfo, this);
        this.userInfo.putAll(userInfo);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        // 用户没有进行有效的设置操作，返回
        if (resultCode == RegistActivity.RESULT_CANCELED) {//取消
            Toast.makeText(context, "取消", Toast.LENGTH_LONG).show();
            return;
        }

        switch (requestCode) {
            case RegistActivity.CODE_GALLERY_REQUEST://如果是来自本地的
                registInteractor.cropRawPhoto(intent.getData(), RegistActivity.output_X, RegistActivity.output_Y);//直接裁剪图片
                break;

            case RegistActivity.CODE_CAMERA_REQUEST:
                registInteractor.cropRawPhoto(tempUri, RegistActivity.output_X, RegistActivity.output_Y);
                break;

            case RegistActivity.CODE_RESULT_REQUEST:
                registInteractor.setImageToHeadView(tempUri, this);//设置图片框
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_regist_main_regist_commit_but:
                if (isCanRegist) {
                    registView.showProgress();
                    registInteractor.registCommit(context, userInfo, this);
                } else {
                    Toast.makeText(context, "请输入正确的信息，换个姿势注册！", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.activity_regist_main_regist_concel_but:
                registView.concelRegist();
                break;

            case R.id.activity_regist_main_avatar_img:
                registView.openSelectAvatar();
                break;

            case R.id.inflate_popwindow_avatar_select_location_but:
                registInteractor.getLocationImage();
                break;
            case R.id.inflate_popwindow_avatar_select_takephoto_but:
                registInteractor.takePhoto();
                break;
            case R.id.inflate_popwindow_avatar_select_cancel_but:
                registView.closeSelectAvatar();
                break;
        }
    }

    @Override
    public void onInputError(int key, String info) {
        registView.setInputError(key, info);
        isCanRegist = false;
    }

    @Override
    public void onInputSuccess() {
        registView.setInputSuccess();
        isCanRegist = true;
    }

    @Override
    public void onRegistError() {
        registView.registError();
        registView.hideProgress();

    }

    @Override
    public void onRegistSuccess() {
        registView.hideProgress();
        registView.registSuccess();
    }


    @Override
    public void onShowAvatar(Bitmap bitmap) {
        registView.setAvatar(bitmap);
        registView.closeSelectAvatar();
    }

    @Override
    public void onUpLoadAvatarComplete() {

    }
}
